﻿using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlatformerCS
{
    /// <summary>
    /// Abstract class for the weapons.
    /// </summary>
    public abstract class Weapon
    {
        protected SpriteFont font;
        protected ContentManager content;
        protected List<Bullet> bullets;
        protected int ammo, ammoMax;
        protected int damage;
        protected int fireRate;
        protected int timer;

        /// <summary>
        /// Makes adding bullets easier.
        /// </summary>
        public struct BulletAddInfo
        {
            // Position of the bullet
            public Vector2 Start;
            public Vector2 End;

            // Orientation of the player
            public int Orientation;

            // To load the texture
            public ContentManager Content;
        }

        /// <summary>
        /// The list of bullet of the current weapon.
        /// </summary>
        public List<Bullet> Bullets
        {
            get { return bullets; }
        }

        public Weapon(ContentManager content)
        {
            this.content = content;
            font = content.Load<SpriteFont>("Fonts/Menu");
        }

        public void Update()
        {
            timer++;

            for (int i = 0; i < bullets.Count; i++)
                bullets[i].Update();

            for (int i = 0; i < bullets.Count; i++)
                if (bullets[i].Wasted)
                    bullets.Remove(bullets[i]);
        }

        /// <summary>
        /// Draw the bullets if there are any.
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void DrawWorld(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < bullets.Count; i++)
                bullets[i].Draw(spriteBatch);
        }

        public void DrawStats(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(font, "Ammo : " + ammo + "/" + ammoMax, Global.AmmoPos, Color.Black);
        }

        /// <summary>
        /// Shoots a bullet at the given coordinate.
        /// The base method checks whether the player has enough ammo\n
        /// and that the weapon isn't in cooldown.
        /// </summary>
        /// <param name="mousePos">Position of the mouse when the shot is triggered.</param>
        public virtual void ShootBullet(BulletAddInfo bulletInfo)
        {
            if (ammo <= 0 || timer < fireRate)
                return;

            ammo--;
            timer = 0;
            /*
            Vector2 start;
            if (bulletInfo.Orientation == 1)
                start = new Vector2(GameBoard.WIDTH / 2, GameBoard.HEIGHT / 2);
            else
                start = new Vector2(GameBoard.WIDTH / 2, GameBoard.HEIGHT / 2);
            */

            Vector2 direction = bulletInfo.End - bulletInfo.Start;
            direction.Normalize();

            Bullet tmp = new Bullet(Bullet.BulletType.GunBullet, bulletInfo.Content, bulletInfo.Start, direction);
            bullets.Add(tmp);
        }
    }
}
